'use strict';
const BaseService = require('./base');

class JsonpService extends BaseService {
  constructor(ctx) {
    super(ctx);
    this.entry = 'jsonp';
  }

  async list(pageCurrent, pageSize, where) {
    const { app } = this;
    const data = await app.mysql.select(this.entry, {
      where,
      order: [[ 'update_at', 'desc' ], [ 'create_at', 'desc' ]],
      offset: (pageCurrent - 1) * pageSize,
      limit: pageSize,
    });
    const create_user = await app.mysql.get('user', { id: data[0].create_user });
    const total = await app.mysql.count(this.entry, where);
    return {
      data: data.map(i => { return { ...i, create_user: create_user.name }; }),
      total,
    };
  }
}

module.exports = JsonpService;
